﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;


namespace BillGuru.UI
{
    class ProductGridViewController
    {
        System.Windows.Forms.DataGridView productGridView;
        
        public ProductGridViewController(System.Windows.Forms.DataGridView productGridView)
        {
            this.productGridView = productGridView;
            this.productGridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.productDataGridView_CellContentClick);

            BindingSource bindingSource = (BindingSource)this.productGridView.DataSource;
            BillGuruDataSet dataSet = (BillGuruDataSet)bindingSource.DataSource;
            productTable = dataSet.Product;


        }
            
        private void productDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == productGridView.Columns["Copy"].Index)
            {
                DialogResult result = input.ShowDialog();
                if (result == DialogResult.Cancel)
                {
                    return;
                }

                int count = int.Parse(input.TextResult);
                MessageBox.Show("Count " + count);

                BillGuruDataSet.ProductRow r = (BillGuruDataSet.ProductRow)productTable.Rows[e.RowIndex];

                for (int c = 0; c < count; ++c)
                {
//                   productTable.AddProductRow(r.UnitOfMeasureRow, r.ProductDescription + "[Clone] " + c , r.QuantityPerUOM, r.Stock, r.SellingPrice, r.DiscountRow);
                }

            }
               
        }

        public void MultiClone()
        {
            int start = productGridView.FirstDisplayedScrollingRowIndex;
            int end = productGridView.DisplayedRowCount(false) + productGridView.FirstDisplayedScrollingRowIndex;


            for (int c = start; c < end; ++c)
            {
                BillGuruDataSet.ProductRow r = (BillGuruDataSet.ProductRow)productTable.Rows[c];
                DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)(productGridView.Rows[c].Cells["dgcSelected"]);
                if (cell.Value != null
                   && Boolean.Parse(cell.Value.ToString()) == true)
                {
                    //                productTable.AddProductRow(r.UnitOfMeasureRow, r.ProductDescription + "[Clone] " + c , r.QuantityPerUOM, r.Stock, r.SellingPrice, r.DiscountRow);
                }

            }
        }

        private FrmInput input = new FrmInput();
        private BillGuruDataSet.ProductDataTable productTable;
    }
}
